import React from 'react';
import {Form, Input, Select, DatePicker, Row, Col, Radio} from 'antd';
import {formItemLayout, respond} from '../_/formItemLayout';
import form from '../_/form';
import {context} from '../_';
import py from '../_/pinyin';
const _fil = formItemLayout.ignore,
    FormItem = Form.Item,
    RadioGroup = Radio.Group,
    Option = Select.Option,
    dataName = 'pdata';
import redux from '../_/redux';
@redux('dict', {
    prefix: 'gender',
    data: 'gender',
    loadArgs: [{params: {type: 'gender'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'marital',
    data: 'marital',
    loadArgs: [{params: {type: 'marital'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'marriage',
    data: 'marriage',
    loadArgs: [{params: {type: 'marriage'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'blood',
    data: 'blood',
    loadArgs: [{params: {type: 'blood'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'patientsource',
    data: 'patientsource',
    loadArgs: [{params: {type: 'patientsource'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'identity',
    data: 'identity',
    loadArgs: [{params: {type: 'identity'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'citizenship',
    data: 'citizenship',
    loadArgs: [{params: {type: 'citizenship'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'nation',
    data: 'nation',
    loadArgs: [{params: {type: 'nation'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'professional',
    data: 'professional',
    loadArgs: [{params: {type: 'professional'}}],
    actions: 'load'
})
@redux('dict', {
    prefix: 'relationship',
    data: 'relationship',
    loadArgs: [{params: {type: 'relationship'}}],
    actions: 'load'
})

@context('params')
@form({
    data: dataName,
    newData: {
        Status: true,
        DisplayOrder: 0
    }
})
export default class patientInfoForm extends React.Component {
    state = {};

    componentWillReceiveProps(props) {
        switch (props.actionTyp) {
        case "pIBsave" :
            this.props.form.validateFieldsAndScroll((errors, values) => {
                if (errors) {
                    props.doAction("err", "pIB", errors);
                    return;
                }
                if (typeof this.props.doAction === 'function') {
                    props.doAction("save", "pIB", values);
                }
            });
            break;
        case "reset":
            this.props.form.resetFields();
            if (typeof this.props.doAction === 'function') {
                props.doAction("reset", "pIB", {});
            }
            break;
        default:
            break;
        }
    }
    checkPatientInfoById = (e) => {
        if (typeof this.props.checkPatientInfoById === 'function') {
            if (e.target.value) {
                this.props.checkPatientInfoById(e.target.value);
            }
        }
    };

    render() {
        const {form} = this.props;
        const {getFieldDecorator: fp} = form,
            disabledDate = function ds(current) {
                return (current && current._d > Date.now());
            },
            pid = fp('PatientId', {
                rules: [
                    {
                        type: "string",
                        required: true,
                        message: '请输入患者ID'
                    }
                ]
            }),
            pName = fp('Name', {
                rules: [
                    {
                        type: "string",
                        required: true,
                        message: '请输入患者姓名'
                    }
                ],
                onChange: (e) => {
                    const value = e.target.value;
                    form.setFieldsValue({NameJP: value ? py(value) : value});
                }
            }),
            pNameJP = fp('NameJP', {
                rules: [{min: 1, max: 200}],
            }),
            gender = fp('Gender', {
                rules: [
                    {
                        type: "string",
                        required: true,
                        message: '请选择患者性别'
                    }
                ]
            }),
            birthday = fp('Birthday', {
                rules: [
                    {
                        type: "object",
                        required: true,
                        message: '请输入出生日期'
                    }
                ]
            }),
            genderItems = this.props.genderdata || [],
            bloodItems = this.props.blooddata || [],
            marriageItems = this.props.marriagedata || [],
            patientsourceItems = this.props.patientsourcedata || [],
            identityItems = this.props.identitydata || [],
            citizenshipItems = this.props.citizenshipdata || [],
            nationItems = this.props.nationdata || [],
            professionalItems = this.props.professionaldata || [],
            relationshipItems = this.props.relationshipdata || [],
            rhBloodItems = [{Id: "001", Name: "阴性"}, {Id: "002", Name: "阳性"}],
            workingConditionItems = [{Id: "003", Name: "在职"}, {Id: "004", Name: "离职"}, {Id: "005", Name: "未知"}];

        return (
            <Form
                horizontal
                onSubmit={this.handleSubmit}
            >
                <h3 className="pFormTitle pftBig">患者基本信息</h3>
                {fp("Id")(<Input type="hidden"/>)}
                <Row>
                    <Col {...respond}>
                        <FormItem
                            label="患者ID"
                            {..._fil}
                        >
                            {pid(
                                <Input
                                    placeholder="请输入患者ID" onBlur={this.checkPatientInfoById}
                                />
                            )}
                        </FormItem>
                        <FormItem
                            label="患者姓名"
                            {..._fil}
                        >
                            {pName(
                                <Input
                                    placeholder="请输入患者姓名"
                                />
                            )}
                        </FormItem>
                        <FormItem
                            label="姓名输入码"
                            {..._fil}
                        >
                            {pNameJP(<Input
                                placeholder="请输入姓名输入码"
                            />)}
                        </FormItem>
                        <FormItem
                            {..._fil}
                            label="性别"
                        >
                            {gender(
                                <RadioGroup>
                                    {genderItems.map(gender =>
                                        <Radio
                                            value={(String)(gender.Code)}
                                            key={`gender_${gender.Code.toString()}`}
                                        >{(String)(gender.Name)}
                                        </Radio>)}
                                </RadioGroup>
                            )}
                        </FormItem>
                        <FormItem
                            {..._fil}
                            label="出生日期"
                        >
                            {birthday(
                                <DatePicker
                                    fomat="YYYY-MM-DD"
                                    disabledDate={disabledDate}
                                />
                            )}
                        </FormItem>
                    </Col>
                    <Col {...respond}>
                        <FormItem
                            {..._fil}
                            label="婚姻"
                        >
                            <Select
                                showSearch
                                placeholder="请选择婚姻状况"
                                optionFilterProp="children"
                                notFoundContent="找不到..."
                            >
                                {marriageItems.map(
                                    marriage =>
                                        <Option
                                            value={(String)(marriage.Id)}
                                            key={`marriage_${marriage.Id.toString()}`}
                                        >
                                            {(String)(marriage.Name)}
                                        </Option>)}
                            </Select>
                        </FormItem>
                        <FormItem
                            {..._fil}
                            label="血型"
                        >
                            <RadioGroup>
                                {bloodItems.map(blood =>
                                    <Radio
                                        value={(String)(blood.Id)}
                                        key={`blood_${blood.Id.toString()}`}
                                    >
                                        {(String)(blood.Name)}
                                    </Radio>)}
                            </RadioGroup>
                        </FormItem>
                        <FormItem
                            {..._fil}
                            label="RH血型"
                        >
                            <RadioGroup>
                                {rhBloodItems.map(rhBlood =>
                                    <Radio
                                        value={(String)(rhBlood.Id)}
                                        key={`rhBlood_${rhBlood.Id.toString()}`}
                                    >
                                        {(String)(rhBlood.Name)}
                                    </Radio>)}
                            </RadioGroup>
                        </FormItem>
                        <FormItem
                            {..._fil}
                            label="患者来源"
                        >
                            <Select
                                showSearch
                                placeholder="请选择患者来源"
                                optionFilterProp="children"
                                notFoundContent="找不到..."
                            >
                                {patientsourceItems.map(patientsource =>
                                    <Option
                                        value={(String)(patientsource.Id)}
                                        key={`patientsource_${patientsource.Id.toString()}`}
                                    >
                                        {(String)(patientsource.Name)}
                                    </Option>)}
                            </Select>
                        </FormItem>
                        <FormItem
                            label="患者条形码"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入患者条形码"
                            />
                        </FormItem>
                    </Col>
                    <Col {...respond}>
                        <FormItem
                            label="身份类别"
                            {..._fil}
                        >
                            <Select
                                showSearch
                                placeholder="请选择身份类别"
                                optionFilterProp="children"
                                notFoundContent="找不到..."
                            >
                                {identityItems.map(identity =>
                                    <Option
                                        value={(String)(identity.Id)}
                                        key={`identity_${identity.Id.toString()}`}
                                    >
                                        {(String)(identity.Name)}
                                    </Option>)}
                            </Select>
                        </FormItem>
                        <FormItem
                            label="身份证号"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入身份证号"
                            />
                        </FormItem>
                        <FormItem
                            label="国籍"
                            {..._fil}
                        >
                            <Select
                                showSearch
                                placeholder="请选择国籍"
                                optionFilterProp="children"
                                notFoundContent="找不到..."
                            >
                                {citizenshipItems.map(citizenship =>
                                    <Option
                                        value={(String)(citizenship.Id)}
                                        key={`citizenship_${citizenship.Id.toString()}`}
                                    >
                                        {(String)(citizenship.Name)}
                                    </Option>)}
                            </Select>
                        </FormItem>
                        <FormItem
                            label="民族"
                            {..._fil}
                        >
                            <Select
                                showSearch
                                placeholder="请选择民族"
                                optionFilterProp="children"
                                notFoundContent="找不到..."
                            >
                                {nationItems.map(nation =>
                                    <Option
                                        value={(String)(nation.Id)}
                                        key={`nation_${nation.Id.toString()}`}
                                    >
                                        {(String)(nation.Name)}
                                    </Option>)}
                            </Select>
                        </FormItem>
                        <FormItem
                            label="籍贯"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入籍贯"
                            />
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <h3 className="pFormTitle">职业信息</h3>
                    <Col {...respond}>
                        <FormItem
                            label="工作单位"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入工作单位"
                            />
                        </FormItem>
                    </Col>
                    <Col {...respond}>
                        <FormItem
                            {..._fil}
                            label="工作状态"
                        >
                            <RadioGroup>
                                {workingConditionItems.map(workingCondition =>
                                    <Radio
                                        value={(String)(workingCondition.Id)}
                                        key={`workingCondition_${workingCondition.Id.toString()}`}
                                    >
                                        {(String)(workingCondition.Name)}
                                    </Radio>)}
                            </RadioGroup>
                        </FormItem>
                    </Col>
                    <Col {...respond}>
                        <FormItem
                            label="职业"
                            {..._fil}
                        >
                            <Select
                                showSearch
                                placeholder="请选择职业"
                                optionFilterProp="children"
                                notFoundContent="找不到..."
                            >
                                {professionalItems.map(professional =>
                                    <Option
                                        value={(String)(professional.Id)}
                                        key={`professional_${professional.Id.toString()}`}
                                    >
                                        {(String)(professional.Name)}
                                    </Option>)}
                            </Select>
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <h3 className="pFormTitle">联系方式</h3>
                    <Col {...respond}>
                        <FormItem
                            label="通讯地址"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入通讯地址"
                            />
                        </FormItem>
                        <FormItem
                            label="现住址"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入现住址"
                            />
                        </FormItem>
                        <FormItem
                            label="户口地址"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入户口地址"
                            />
                        </FormItem>
                        <FormItem
                            label="出生地"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入出生地"
                            />
                        </FormItem>
                    </Col>
                    <Col {...respond}>
                        <FormItem
                            label="email"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入email"
                            />
                        </FormItem>
                        <FormItem
                            label="住址邮编"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入住址邮编"
                            />
                        </FormItem>
                        <FormItem
                            label="户口邮编"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入户口邮编"
                            />
                        </FormItem>
                        <FormItem
                            label="电话"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入电话"
                            />
                        </FormItem>
                    </Col>
                    <Col {...respond}>
                        <FormItem
                            label="联系人姓名"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入联系人姓名"
                            />
                        </FormItem>
                        <FormItem
                            label="联系人电话"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入联系人电话"
                            />
                        </FormItem>
                        <FormItem
                            label="联系人地址"
                            {..._fil}
                        >
                            <Input
                                placeholder="请输入联系人地址"
                            />
                        </FormItem>
                        <FormItem
                            label="联系人关系"
                            {..._fil}
                        >
                            <Select
                                showSearch
                                placeholder="请选择联系人关系"
                                optionFilterProp="children"
                                notFoundContent="找不到..."
                            >
                                {relationshipItems.map(relationship =>
                                    <Option
                                        value={(String)(relationship.Id)}
                                        key={`relationship_${relationship.Id.toString()}`}
                                    >
                                        {(String)(relationship.Name)}
                                    </Option>)}
                            </Select>
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        );
    }
}
